package com.codec;

import java.io.UnsupportedEncodingException;


public class MessagePack {
	
	private int length;	
	private int type;	
	private int requestid;	
	private String content;
	
	public MessagePack(){		
	}
	
	public MessagePack(int type, String content) {
		try {
			this.type = type;
			this.content = content;
			int len = content==null?0:content.getBytes("utf-8").length;
			this.length = 8 + len;
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}
	
	public MessagePack(int type, int requestid, String content) {
		try {
			this.type = type;
			this.requestid = requestid;
			this.content = content;
			int len = content == null?0:content.getBytes("utf-8").length;
			this.length = 12 + len;
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}
	
	public MessagePack(byte[] bytData) {
		if(bytData!=null && bytData.length>=8) {
			length = ByteUtil.getIntForIOS(bytData, 0);
			type = ByteUtil.getIntForIOS(bytData, 4);
			content = new String(bytData,8,bytData.length-8);
		}
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public int getLength() {
		return length;
	}

	public void setLength(int length) {
		this.length = length;
	}

	public int getRequestid() {
		return requestid;
	}

	public void setRequestid(int requestid) {
		this.requestid = requestid;
	}

}
